package com.spdzen.support;

import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;

public class SimpleServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M, T> implements SimpleService<T> {
	@Override
	public T selectOne(T record) {
		return baseMapper.selectOne(record);
	}

	@Override
	public int delete(T record) {
		Wrapper<T> wrapper = null;
		if(record == null) {
			wrapper = new EntityWrapper<T>();
		} else {
			wrapper = new EntityWrapper<T>(record);
		}
		return baseMapper.delete(wrapper);
	}

	@Override
	public int selectCount(T record) {
		Wrapper<T> wrapper = null;
		if(record == null) {
			wrapper = new EntityWrapper<T>();
		} else {
			wrapper = new EntityWrapper<T>(record);
		}
		
		return baseMapper.selectCount(wrapper);
	}
	
	@Override
	public int selectCount() {
		Wrapper<T> wrapper = new EntityWrapper<T>();		
		return baseMapper.selectCount(wrapper);
	}

	@Override
	public List<T> selectList(T record) {
		if(record == null) {
			return baseMapper.selectList(null);
		} else {
			return baseMapper.selectList(new EntityWrapper<T>(record));
		}		
	}
	
	@Override
	public List<T> selectAll() {
		return baseMapper.selectList(null);
	}

	@Override
	public SimplePage<T> selectPage(PageOffsetParam pageParam) {
		PageHelper.offsetPage(PageUtils.checkOffset(pageParam.getOffset()), PageUtils.checkLimit(pageParam.getLimit()));
		List<T> rows = baseMapper.selectList(new EntityWrapper<>());
		return PageUtils.toSimplePage(rows);
	}
	
	@Override
	public SimplePage<T> selectPage(PageOffsetParam pageParam, String orderBy) {
		PageHelper.offsetPage(PageUtils.checkOffset(pageParam.getOffset()), PageUtils.checkLimit(pageParam.getLimit()));
		
		Wrapper<T> wrapper = new EntityWrapper<>();
		
		if(StringUtils.isNotBlank(orderBy)){
			wrapper.orderBy(orderBy);
		}
		
		List<T> rows = baseMapper.selectList(wrapper);
		return PageUtils.toSimplePage(rows);
	}

	@Override
	public SimplePage<T> selectPage(PageOffsetParam pageParam, T record) {
		PageHelper.offsetPage(PageUtils.checkOffset(pageParam.getOffset()), PageUtils.checkLimit(pageParam.getLimit()));		
		
		Wrapper<T> wrapper = null;
		
		if(record == null){
			wrapper = new EntityWrapper<>();
		}else{
			wrapper = new EntityWrapper<>(record);
		}
		
		List<T> rows = baseMapper.selectList(wrapper);
		
		return PageUtils.toSimplePage(rows);
	}
	
	@Override
	public SimplePage<T> selectPage(PageOffsetParam pageParam, T record, String orderBy) {
		PageHelper.offsetPage(PageUtils.checkOffset(pageParam.getOffset()), PageUtils.checkLimit(pageParam.getLimit()));		
		
		Wrapper<T> wrapper = null;
		
		if(record == null){
			wrapper = new EntityWrapper<>();
		}else{
			wrapper = new EntityWrapper<>(record);
		}
		
		if(StringUtils.isNotBlank(orderBy)){
			wrapper.orderBy(orderBy);
		}
		
		List<T> rows = baseMapper.selectList(wrapper);
		
		return PageUtils.toSimplePage(rows);
	}
	
	@Override
	public SimplePage<T> selectPage(PageNumParam pageParam) {
		int pageNum = PageUtils.checkPageNum(pageParam.getPageNum());
		int pageSize = PageUtils.checkPageSize(pageParam.getPageSize());
		PageHelper.startPage(pageNum, pageSize);
		
		Wrapper<T> wrapper = new EntityWrapper<>();
		List<T> rows = baseMapper.selectList(wrapper);
		
		return PageUtils.toSimplePage(rows);
	}
	
	@Override
	public SimplePage<T> selectPage(PageNumParam pageParam, String orderBy) {
		int pageNum = PageUtils.checkPageNum(pageParam.getPageNum());
		int pageSize = PageUtils.checkPageSize(pageParam.getPageSize());
		PageHelper.startPage(pageNum, pageSize);
		
		Wrapper<T> wrapper = new EntityWrapper<>();
		
		if(StringUtils.isNotBlank(orderBy)){
			wrapper.orderBy(orderBy);
		}
		
		List<T> rows = baseMapper.selectList(wrapper);
		
		return PageUtils.toSimplePage(rows);
	}

	@Override
	public SimplePage<T> selectPage(PageNumParam pageParam, T record) {
		int pageNum = PageUtils.checkPageNum(pageParam.getPageNum());
		int pageSize = PageUtils.checkPageSize(pageParam.getPageSize());
		PageHelper.startPage(pageNum, pageSize);
		
		Wrapper<T> wrapper = null;
		
		if(record == null){
			wrapper = new EntityWrapper<>();
		}else{
			wrapper = new EntityWrapper<>(record);
		}
		
		List<T> rows = baseMapper.selectList(wrapper);
		
		return PageUtils.toSimplePage(rows);
	}
	
	@Override
	public SimplePage<T> selectPage(PageNumParam pageParam, T record, String orderBy) {
		int pageNum = PageUtils.checkPageNum(pageParam.getPageNum());
		int pageSize = PageUtils.checkPageSize(pageParam.getPageSize());
		PageHelper.startPage(pageNum, pageSize);
		
		Wrapper<T> wrapper = null;
		
		if(record == null){
			wrapper = new EntityWrapper<>();
		}else{
			wrapper = new EntityWrapper<>(record);
		}
		
		if(StringUtils.isNotBlank(orderBy)){
			wrapper.orderBy(orderBy);
		}
		
		List<T> rows = baseMapper.selectList(wrapper);
		
		return PageUtils.toSimplePage(rows);
	}
	
}
